package com.example.demo.Controller;

import com.example.common.redis.RedisCache;
import com.example.common.utils.AjaxResult;
import com.example.demo.domain.DTO.ProvinceHot;
import com.example.demo.domain.ProvinceInfo;
import com.example.demo.domain.ProvincePointCity;
import com.example.demo.service.ProvinceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 省份Controller
 */
@RestController
@RequestMapping("/api/other/province")
//@RequestMapping("/province")
@EnableAsync
public class ProvinceController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ProvinceInfoService provinceInfoService;

    /**
     * 热门省份
     *
     * @return
     */
    @GetMapping("/hotTop")
    public AjaxResult hotTop() {
        try {
            //查询redis是否存在数据
            List<ProvinceHot> redisInfo = redisCache.getCacheObject("hotTop");
            //如果缓存中已存在则直接返回
            return AjaxResult.success("查询成功", redisInfo);
        } catch (NullPointerException e) {
            List<ProvinceHot> provinceHots = provinceInfoService.selectProvinceHot();
            //缓存时间30分钟
            redisCache.setCacheJSONObject("hotTop", provinceHots, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", provinceHots);
        }
    }


    /**
     * 返回所有的省份名
     *
     * @return
     */
    @GetMapping("/provinceList")
    public AjaxResult provinceList() {
        try {
            //查询redis是否存在数据
            List<ProvinceHot> redisInfo = redisCache.getCacheObject("provinceList");
            //如果缓存中已存在则直接返回
            return AjaxResult.success("查询成功", redisInfo);
        } catch (NullPointerException e) {
            List<ProvinceInfo> provinceInfos = provinceInfoService.selectProvinceInfo();
            //缓存时间30分钟
            redisCache.setCacheJSONObject("provinceList", provinceInfos, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", provinceInfos);
        }
    }

    /**
     * 返回指定省份的所有城市
     *
     * @return
     */
    @PostMapping("/provincePointCity")
    public AjaxResult provincePointCity(String provinceName) {
        try {
            //查询redis是否存在数据
            List<ProvinceHot> redisInfo = redisCache.getCacheObject("provincePointCity_" + provinceName);
            //如果缓存中已存在则直接返回
            return AjaxResult.success("查询成功", redisInfo);
        } catch (NullPointerException e) {
            List<ProvincePointCity> provincePointCities = provinceInfoService.selectProvincePointCity(provinceName);
            //缓存时间30分钟
            redisCache.setCacheJSONObject("provincePointCity_" + provinceName, provincePointCities, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", provincePointCities);
        }
    }
}
